package com.lzw.d25bingso;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class InfoActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,ViewPager.OnPageChangeListener{
    private RadioGroup rg;
    private RadioButton [] rbs;
    private ViewPager viewPager;
    private List<Fragment> data;
    private PageAdapter adapter;
    private boolean isQuit;
    private Timer timer = new Timer();
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        viewPager = (ViewPager) findViewById(R.id.view_pager_id2);
        rg = (RadioGroup) findViewById(R.id.rg_id);

        data = new ArrayList<>();
         MyFragment myFragment = new MyFragment();
         data.add(myFragment);
        MyFragment myFragment2 = new MyFragment();
        data.add(myFragment2);
        MyFragment myFragment3 = new MyFragment();
        data.add(myFragment3);
        MyFragment myFragment4 = new MyFragment();
        data.add(myFragment4);

        adapter = new PageAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        initRadioButton();
        viewPager.addOnPageChangeListener(this);

    }

    private void initRadioButton() {
        rg.setOnCheckedChangeListener(this);
        rbs = new  RadioButton[data.size()];
        for (int i = 0; i<rbs.length;i++){
           rbs[i]= (RadioButton) rg.getChildAt(i);

        }
        rbs[0].setChecked(true);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        for (int i =0; i<rbs.length;i++){
            if (rbs[i].getId() == checkedId){
                viewPager.setCurrentItem(i);
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    @Override
    public void onPageSelected(int position) {
        for (int i= 0 ;i<rbs.length;i++){
            rbs[i].setChecked(false);
        }
        rbs[position].setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {}



    public class PageAdapter extends FragmentPagerAdapter{

        public PageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return data.get(position);
        }

        @Override
        public int getCount() {
            return data.size();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (!isQuit) {
                isQuit = true;
                Toast.makeText(this, "再按返回键将退出本程序", Toast.LENGTH_SHORT).show();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        isQuit = false;
                    }
                }, 2000);
            }else {
                System.exit(0);
            }
        }
        return false;

    }
}
